package com.hardware;

public class Clock {
    private static Clock instance;
    private int currentTime;

    private Clock() {
        currentTime = 0;
    }

    public static synchronized Clock getInstance() {
        if (instance == null) {
            instance = new Clock();
            instance.start();
        }
        return instance;
    }

    private void start() {
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000); // 线程休眠1秒
                    increaseTime();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    private synchronized void increaseTime() {
        currentTime++;
    }

    public int getCurrentTime() {
        return currentTime;
    }
}